package cn.edu.scau.dropship.controller;

import cn.edu.scau.dropship.entity.Company;
import cn.edu.scau.dropship.entity.User;
import cn.edu.scau.dropship.service.CompanyService;
import cn.edu.scau.dropship.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Controller
@RequestMapping("/company")
public class CompanyController {
    @Autowired
    private CompanyService companyService;

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/addCompany")
    @ResponseBody
    public String addCompany(String Chinese_name, String English_name, String brief_introduction, String report_type, String url, Integer mvoId) {
        List<Company> list = companyService.selectAll();
        for (Company company : list) {
            if (company.getChineseName().equals(Chinese_name)) {
                return "该公司中文名称已被注册，请重试";
            } else if (company.getEnglishName().equals(English_name)) {
                return "该公司英文名称已被注册，请重试";
            }
        }
        Company company=new Company(null,Chinese_name ,English_name , brief_introduction, report_type , url);
        int re = companyService.insert(company);
        if (re == 1) {
            Company company1 = companyService.selectByEname(English_name);
            User user = userService.selectByPrimaryKey(mvoId);
            user.setCompanyId(company1.getId());
            int result=userService.updateByPrimaryKey(user);
            if(result>0){
                return "添加公司讯息成功";
            }
            return "添加公司讯息失败";
        }
        return "添加公司讯息失败";
    }


    @PostMapping("/searchCompanyByID")
    public Company searchCompany(Integer id) {
        Company company = companyService.selectByPrimaryKey(id);
        return company;
    }


    @PostMapping("/updateCompany")
    @ResponseBody
    public String updateCompany(Integer id, String Chinese_name, String English_name, String brief_introduction, String report_type, String url) {
        List<Company> list = this.getAllCompany();
        for (Company company : list) {
            if (id == company.getId()) continue;
            if (company.getChineseName().equals(Chinese_name)) {
                return "该公司中文名称已被存在，请重试";
            } else if (company.getEnglishName().equals(English_name)) {
                return "该公司英文名称已被存在，请重试";
            }
        }
        Company company = new Company(id, Chinese_name, English_name, brief_introduction, report_type, url);
        int re = companyService.updateByPrimaryKey(company);
        if (re == 1) return "成功更新公司讯息";
        return "更新公司讯息失败";
    }


    @RequestMapping("/getAllCompany")
    public List<Company> getAllCompany() {
        return companyService.selectAll();
    }


    public Company getCompanyIDByName(String name) {
        return companyService.selectByEname(name);
    }
}
